package com.example.task1.ui.AllService.life;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.LifeWeatherBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import retrofit2.Call;

public class LifeWeatherActivity extends AppCompatActivity {


    private TextView tvTemperature;
    private TextView tvApparentTemperature;
    private TextView tvWeather;
    private TextView tvMinAndMax;
    private RecyclerView tvTime;
    private RecyclerView tvDayTime;
    private TextView tvWindDirection;
    private TextView tvWindStrength;
    private TextView tvHumidity;
    private TextView tvUv;
    private TextView tvSportIndex;
    private TextView tvDressingIndex;
    private LifeWeatherTimeAdapter lifeWeatherTimeAdapter;
    private LifeWeatherDayAdapter lifeWeatherDayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_life_weather);
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle("天气");
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        initView();
        initData();
        setAdapter();
    }

    private void setAdapter() {
        LinearLayoutManager timeLayoutManager = new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false);
        LinearLayoutManager dayLayoutManager = new LinearLayoutManager(this);
        lifeWeatherTimeAdapter = new LifeWeatherTimeAdapter(this);
        tvTime.setAdapter(lifeWeatherTimeAdapter);
        tvTime.setLayoutManager(timeLayoutManager);
        lifeWeatherDayAdapter = new LifeWeatherDayAdapter(this);
        tvDayTime.setLayoutManager(dayLayoutManager);
        tvDayTime.setAdapter(lifeWeatherDayAdapter);
    }

    private void initData() {
        Call<ApiResp> apiRespCall = Api.api().get(MyApplication.TOKEN, "/prod-api/api/living/weather");
        Api.send(apiRespCall, LifeWeatherBean.class, new CallBack<LifeWeatherBean>() {
            @Override
            public void success(LifeWeatherBean data) {
                LifeWeatherBean.DataDTO rows = data.getData();
                lifeWeatherTimeAdapter.setData(data.getData().getToday().getHours());
                lifeWeatherDayAdapter.setData(data.getData().getWeatherList());
                tvTemperature.setText(rows.getToday().getTempInfo().getTemperature());
                tvApparentTemperature.setText("体感\n"+rows.getToday().getTempInfo().getApparentTemperature()+"℃");
                tvDressingIndex.setText(rows.getToday().getComfortLevel().getDressingIndex());
                tvHumidity.setText(rows.getToday().getComfortLevel().getHumidity()+"");
                tvWeather.setText(rows.getToday().getTempInfo().getWeather());
                tvWindDirection.setText(rows.getToday().getWind().getWindDirection());
                tvWindStrength.setText(rows.getToday().getWind().getWindStrength());
                tvMinAndMax.setText(rows.getToday().getTempInfo().getMaxTemperature() + " ~ " +
                        rows.getToday().getTempInfo().getMinTemperature());
                tvUv.setText(rows.getToday().getTempInfo().getUv());
                tvSportIndex.setText(rows.getToday().getComfortLevel().getSportIndex());
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        tvTemperature = (TextView) findViewById(R.id.tv_temperature);
        tvApparentTemperature = (TextView) findViewById(R.id.tv_apparentTemperature);
        tvWeather = (TextView) findViewById(R.id.tv_weather);
        tvMinAndMax = (TextView) findViewById(R.id.tv_minAndMax);
        tvTime = (RecyclerView) findViewById(R.id.tv_time);
        tvDayTime = (RecyclerView) findViewById(R.id.tv_dayTime);
        tvWindDirection = (TextView) findViewById(R.id.tv_windDirection);
        tvWindStrength = (TextView) findViewById(R.id.tv_windStrength);
        tvHumidity = (TextView) findViewById(R.id.tv_humidity);
        tvUv = (TextView) findViewById(R.id.tv_uv);
        tvSportIndex = (TextView) findViewById(R.id.tv_sportIndex);
        tvDressingIndex = (TextView) findViewById(R.id.tv_dressingIndex);
    }
}